package com.krmes.mes.app;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;

import com.krmes.mes.R;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class AppConfig
{
    public static String LOCAL_PATH = "/config.properties";
    public static String Server_IP = "10.1.32.12";
    public static String Server_Port = "8080";
    private static Properties properties;

    private boolean checkValidateConfig()
    {
        return (Server_IP != null) && (Server_Port != null);
    }

    public static AppConfig getAppConfig(Context context)
    {
        LOCAL_PATH = context.getFilesDir().getPath() + "/config.properties";
        return SingletonHolder.instance;
    }

    public static boolean isExistExternalStore()
    {
        return Environment.getExternalStorageState().equals("mounted");
    }

    private void loadConfigByProperties()
    {
        Server_IP = properties.getProperty("server_ip");
        Server_Port = properties.getProperty("server_port");
    }

    /* Error */
    private void setProps(Properties p)  {

        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream(new File(LOCAL_PATH));

            p.store(fos, null);
            fos.flush();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                fos.close();
            } catch (Exception e) {}
        }
    }

    public String get(String key)
    {
        Properties props = props = get();
        return props != null ? props.getProperty(key) : null;
    }

    /* Error */
    public Properties get()  {
        Properties props = new Properties();
        try {
            FileInputStream fis2 = new FileInputStream(LOCAL_PATH);
            props.load(fis2);
            fis2.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return props;
    }

    public String getExternalStorePath()
    {
        if (isExistExternalStore()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return null;
    }

    public SharedPreferences getSharedPreferences(Context paramContext)
    {
        return PreferenceManager.getDefaultSharedPreferences(paramContext);
    }

    public boolean initProperties(Context paramContext)
    {
        if (properties == null) {
            properties = new Properties();
        }
        if ((isExistExternalStore()) && (readContentByFile(LOCAL_PATH) != null)) {
            try
            {
                properties.load(new FileInputStream(LOCAL_PATH));
                loadConfigByProperties();
                boolean bool2 = checkValidateConfig();
                return bool2;
            }
            catch (IOException localIOException)
            {
                localIOException.printStackTrace();
                return false;
            }
        }
        try
        {
            properties.load(paramContext.getResources().openRawResource(R.raw.config));
            loadConfigByProperties();
            boolean bool1 = checkValidateConfig();
            return bool1;
        }
        catch (Exception localException)
        {
            localException.printStackTrace();
        }
        return false;
    }

    /* Error */
    public String readContentByFile(String path)
    {
       // BufferedReader reader = null;
            File file = new File(path);
//            if (file.exists()) {
//                StringBuilder sb = new StringBuilder();
//                BufferedReader reader2 = new BufferedReader(new FileReader(file));
//                String line = reader2.readLine();
//
//                sb.append(line.trim());
//                if (reader2 != null) {
//                    reader2.close();}
//            }
        //sb.toString().trim()

        return file.exists()?path:null;
    }

    public void remove(String... key)
    {
        Properties props = get();
        for (String k : key) {
            props.remove(k);
        }
        setProps(props);
    }

    public void set(String paramString1, String paramString2)
    {
        Properties localProperties = get();
        localProperties.setProperty(paramString1, paramString2);
        setProps(localProperties);
    }

    public void set(Properties paramProperties)
    {
        Properties localProperties = get();
        localProperties.putAll(paramProperties);
        setProps(localProperties);
    }

    public void setProperties(Properties paramProperties)
    {
        set(paramProperties);
    }

    private static class SingletonHolder
    {
        private static AppConfig instance = new AppConfig();
    }
}
